
#include "ConfigManager.h"

extern "C"
{
    #include "utility.h"
};

#include "stdio.h"
#define key_name L"name"
#define key_db_path L"db_path"
#define key_log_path L"log_path"
#define key_size L"size"
#define key_memdb L"memdb"
#define key_database L"database"
#define key_version L"version"
#define key_max_connection L"max_connection"
#define key_dba_name L"dba_name"
#define key_dba_pwd L"dba_pwd"
#define key_user  L"user"
#define key_pwd  L"pwd"
#define key_create_time L"create_time"
#define key_unsed_size L"unused_size"
#define key_port  L"port"


ConfigManager::ConfigManager(void)
{
	wstring filePath = CONFIG_FILE_PATH;
	this->ReadIn(filePath);
}

ConfigManager::~ConfigManager(void)
{

}

void ConfigManager::LoadConfigInfo()
{
	max_connection = this->GetRootElem()->GetAttribute(key_max_connection);
	dba_name = this->GetRootElem()->GetAttribute(key_dba_name);
	dba_pwd = this->GetRootElem()->GetAttribute(key_dba_pwd);
	str_port = this->GetRootElem()->GetAttribute(key_port);
	vector<XmlElement*>* pvec_db = (vector<XmlElement*>*)this->GetRootElem()->GetChildElement();
	vector<XmlElement*>::iterator it_db = pvec_db->begin();
	while(it_db != pvec_db->end())
	{
		db d;
		d.name = (*it_db)->GetAttribute(key_name);
		d.db_path = (*it_db)->GetAttribute(key_db_path);
		d.log_path = (*it_db)->GetAttribute(key_log_path);
		d.db_size = (*it_db)->GetAttribute(key_size);
		d.db_unused_size = (*it_db)->GetAttribute(key_unsed_size);
		d.user = (*it_db)->GetAttribute(key_user);
		d.pwd = (*it_db)->GetAttribute(key_pwd);
		d.create_time = (*it_db)->GetAttribute(key_create_time);
		list_db.push_back(d);
		++ it_db;
	}
}

list<db>& ConfigManager::GetListDB()
{
	return list_db;
}

std::wstring ConfigManager::GetMaxConnection()
{
	return max_connection;
}

std::wstring ConfigManager::GetDBAName()
{
	return dba_name;
}

std::wstring ConfigManager::GetDBAPwd()
{
	return dba_pwd;
}


bool ConfigManager::AddDatabase( db cdb )
{
	XmlElement* pElem = new XmlElement();
	pElem->SetName(key_database);
	pElem->AddAttr(key_name, cdb.name);
	pElem->AddAttr(key_create_time, cdb.create_time);
	pElem->AddAttr(key_db_path, cdb.db_path);
	pElem->AddAttr(key_log_path, cdb.log_path);
	pElem->AddAttr(key_size, cdb.db_size);
	pElem->AddAttr(key_unsed_size, cdb.db_unused_size);
	pElem->AddAttr(key_user, cdb.user);
	pElem->AddAttr(key_pwd, cdb.pwd);
	this->GetRootElem()->GetChildElement()->push_back(pElem);
	return true;
}

void ConfigManager::Save()
{	
	this->OutPut(CONFIG_FILE_PATH);
}

std::wstring ConfigManager::GetPort()
{
	return str_port;
}